package com.rop.znyx.activity.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rop.znyx.activity.service.CouponInfoService;
import com.rop.znyx.common.result.Result;
import com.rop.znyx.model.activity.CouponInfo;
import com.rop.znyx.vo.activity.CouponRuleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 优惠券信息 前端控制器
 * </p>
 *
 * @author rop
 * @since 2023-07-09
 */

@Api(tags = "优惠券列表管理")
@RestController
@RequestMapping("admin/activity/couponInfo")
@RequiredArgsConstructor

public class CouponInfoController {

    private final CouponInfoService couponInfoService;

    /**
     * 获取分页列表
     */
    @ApiOperation(value = "获取分页列表")
    @GetMapping("{page}/{limit}")
    public Result<IPage<CouponInfo>> index(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {

        Page<CouponInfo> pageParam = new Page<>(page, limit);
        IPage<CouponInfo> pageModel = couponInfoService.selectPage(pageParam);

        return Result.ok(pageModel);
    }

    /**
     * 获取优惠券
     */
    @ApiOperation(value = "获取优惠券")
    @GetMapping("get/{id}")
    public Result<CouponInfo> get(@PathVariable String id) {
        CouponInfo couponInfo = couponInfoService.getCouponInfo(id);
        return Result.ok(couponInfo);
    }

    /**
     * 新增优惠券
     */
    @ApiOperation(value = "新增优惠券")
    @PostMapping("save")
    public Result<Boolean> save(@RequestBody CouponInfo couponInfo) {
        boolean isSuccess = couponInfoService.save(couponInfo);
        return isSuccess ? Result.ok() : Result.fail();
    }

    /**
     * 修改优惠券
     */
    @ApiOperation(value = "修改优惠券")
    @PutMapping("update")
    public Result<Boolean> updateById(@RequestBody CouponInfo couponInfo) {
        boolean isSuccess = couponInfoService.updateById(couponInfo);
        return isSuccess ? Result.ok() : Result.fail();
    }

    /**
     * 删除优惠券
     */
    @ApiOperation(value = "删除优惠券")
    @DeleteMapping("remove/{id}")
    public Result<Boolean> remove(@PathVariable String id) {
        boolean isSuccess = couponInfoService.removeById(id);
        return isSuccess ? Result.ok() : Result.fail();
    }

    /**
     * 根据id列表删除优惠券
     */
    @ApiOperation(value = "根据id列表删除优惠券")
    @DeleteMapping("batchRemove")
    public Result<Boolean> batchRemove(@RequestBody List<String> idList) {
        boolean isSuccess = couponInfoService.removeByIds(idList);
        return isSuccess ? Result.ok() : Result.fail();
    }

    /**
     * 获取优惠券信息
     */
    @ApiOperation(value = "获取优惠券信息")
    @GetMapping("findCouponRuleList/{id}")
    public Result<Map<String, Object>> findActivityRuleList(@PathVariable Long id) {
        return Result.ok(couponInfoService.findCouponRuleList(id));
    }

    /**
     * 新增活动
     */
    @ApiOperation(value = "新增活动")
    @PostMapping("saveCouponRule")
    public Result<Void> saveCouponRule(@RequestBody CouponRuleVo couponRuleVo) {
        couponInfoService.saveCouponRule(couponRuleVo);
        return Result.ok();
    }

    /**
     * 根据关键字获取sku列表，活动使用
     */
    @ApiOperation(value = "根据关键字获取sku列表，活动使用")
    @GetMapping("findCouponByKeyword/{keyword}")
    public Result<List<CouponInfo>> findCouponByKeyword(@PathVariable("keyword") String keyword) {
        return Result.ok(couponInfoService.findCouponByKeyword(keyword));
    }
}

